package javase.collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ToArrayTest {

	public static void main(String[] args) {
		List<Integer> list = initList();
		printList(list);
		
		Integer[] array = list.toArray(new Integer[0]);
		System.out.println(array);
		printIntArray(array);
		
		array[0] = new Integer(999);

		printList(list);
		printIntArray(array);
	}

	private static void printList(List<Integer> list) {
	    System.out.println("List = " + list);
	}

	private static List<Integer> initList() {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < 10; i++) {
			list.add(new Integer(i));
		}
		return list;
	}
	
	private static void printIntArray(Integer [] array) {
	    StringBuilder arrayAsString = new StringBuilder("Array = [");
	    for (int i = 0; i < array.length; i++) {
		arrayAsString.append(array[i]);
		if (i + 1 < array.length) {
		    arrayAsString.append(", ");
		}
	    }
	    arrayAsString.append("]");
	    System.out.println(arrayAsString.toString());
	}

}
